package com.peachcastle.tools
{
	
	import flash.accessibility.AccessibilityProperties;
	import flash.display.*;
	import flash.events.*;
	import flash.geom.Point;
	import flash.text.*;
	
	/**
	 * ToolTip.init(容器);//初始化
	 * ToolTip.register(目标,提示信息);//注册
	 *  
	 */	
	public class DToolTip extends Sprite
	{
		
		public static const TOP:String = "top";
		
		public static const BOTTOM:String = "bottom";
		
		public static const LEFT:String = "left";
		
		public static const RIGHT:String = "right";
		
		public static const MIDDLE:String = "middle";
		
		
		private static var instance:DToolTip = null;
		private var label:TextField;		
		private var area:DisplayObject;
		public function DToolTip() 
		{
			label = new TextField();
			label.autoSize = TextFieldAutoSize.RIGHT;
			label.selectable = false;
			label.multiline = true;
			label.wordWrap = true;
			label.defaultTextFormat = new TextFormat("宋体", 12, 0x666666);
			label.text = "提示信息";
			label.x = 5;
			label.y = 2;
			//label.height = 20;
			addChild(label);
			redraw();
			visible = false;
			mouseEnabled = mouseChildren = false;
		}
		
		private function redraw():void
		{
			var w:Number = 5 + label.width;
			var h:Number = 5 + label.height;			
			this.graphics.clear();
			this.graphics.beginFill(0x000000, 0.4);
			this.graphics.drawRoundRect(3, 3, w, h, 5, 5);
			this.graphics.endFill();
			this.graphics.beginFill(0xffffff);
			this.graphics.drawRoundRect(0, 0, w, h, 5, 5);
			this.graphics.endFill();
		}
		/**
		 * 初始化;
		 * @param	base  容器
		 */
		public static function init(base:DisplayObjectContainer, isRemovePre:Boolean = false):void
		{
			if (isRemovePre && instance) instance = null;
			
			if (instance == null) 
			{
				instance = new DToolTip();
				base.addChild(instance);
			}
		}
		
		/**
		 * 注册
		 * @param	area
		 * @param	message
		 */
		public static function register(area:DisplayObject, message:String, dictory:String = "top"):void 
		{
			if (instance != null) 
			{
				var prop:AccessibilityProperties = new AccessibilityProperties();
				prop.description = message;
				prop.name = dictory;
				area.accessibilityProperties = prop;
				area.addEventListener(MouseEvent.MOUSE_OVER, instance.handler);
			}
		}
		
		
		
		/**
		 * 销毁 
		 * @param area
		 * 
		 */		
		public static function unregister(area:DisplayObject):void {
			if (instance != null) {
				area.removeEventListener(MouseEvent.MOUSE_OVER, instance.handler);
			}
		}
		
		
		public function show(area:DisplayObject):void {
			this.area = area;
			this.area.addEventListener(MouseEvent.MOUSE_OUT, this.handler);
			//this.area.addEventListener(MouseEvent.MOUSE_MOVE, this.handler);	
			if (area.accessibilityProperties)
			{
				label.text = area.accessibilityProperties.description;
			}
			if (label.text.length > 10) 
			{
				label.width = 100;
			}
			else
			{
				label.width = label.text.length * 14;
			}
			redraw();			
		}
		
		public function hide():void	
		{
			this.area.removeEventListener(MouseEvent.MOUSE_OUT, this.handler);
			//this.area.removeEventListener(MouseEvent.MOUSE_MOVE, this.handler);
			this.area = null;
			visible = false;
		}
		
		public function move(point:Point):void {			 
			var lp:Point = this.parent.globalToLocal(point);
			this.x = lp.x - this.width/2;			
			this.y = lp.y + label.height;
			if (!visible)
			{
				visible = true;
			}
		}
		
		private function handler(event:MouseEvent):void	
		{
			switch(event.type) {
				case MouseEvent.MOUSE_OUT:
					this.hide();
					break;
				case MouseEvent.MOUSE_OVER:
					var target:DisplayObject = event.currentTarget as DisplayObject;
					var p:Point = null;
					if (target.accessibilityProperties)
					{
						switch(target.accessibilityProperties.name)
						{
							case TOP:
								p = new Point(target.x, target.y - target.height/2);
								break;
							case BOTTOM:
								p = new Point(target.x, target.y  + target.height/2);
								break;
							case LEFT:
								p = new Point(target.x - target.width/2, target.y);
								break;
							case RIGHT:
								p = new Point(target.x + target.width/2, target.y);
								break;
							case MIDDLE:
							default:
								p = new Point(target.x, target.y);
								break;
						}
					}else
					{
						p = new Point(target.x, target.y);
					}
					this.show(target);
					this.move(p)
					break;
			}
		}
		
	}
}